package com.example.leavejava.service;


import com.example.leavejava.Result;
import com.example.leavejava.domain.Add;
import com.example.leavejava.domain.Search;
import com.example.leavejava.mapper.Leave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class LeaveService {
    @Autowired
    private Leave leave;

    @Transactional
    public Result AddLeave(Add add){
        Result result = new Result();
        String name = add.getName();
        String remark = add.getRemark();
        if(name == null || name == ""){
            result.setResult("error");
            result.setMsg("名字不能为空");
            return result;
        }
        if(remark == null || remark == ""){
            result.setResult("error");
            result.setMsg("留言不能为空");
            return result;
        }
        Long timeStamp = System.currentTimeMillis();  //获取当前时间戳
        SimpleDateFormat setLeaveDate = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        String setLeaveDate1 = setLeaveDate.format(new Date(Long.parseLong(String.valueOf(timeStamp))));

        add.setLeaveDate(setLeaveDate1);
        leave.save(add);
        result.setResult("success");
        result.setMsg("添加成功");
        return result;
    }

    public Result SearchLeave(Search search){
        Result result = new Result();
        String name = search.getName();
        if(name == null || name == ""){
            result.setResult("error");
            result.setMsg("请输入你想说话的人");
            return result;
        }
        List<Search> searchData = leave.selectList(search);
        result.setResult("success");
        result.setData(searchData);
        result.setMsg("查询成功");
        return result;
    }
}
